use strict;
use warnings;
use utf8;
use HTTP::DAV;

webdav_main( @ARGV );

sub read_ini{
	my ( $ini_file ) = @_;
	my $h_ini_file = require $ini_file if( defined $ini_file && -e $ini_file );
	return $h_ini_file;
}

sub get_parameter{
	my ( $ini_file, @args ) = @_;
	my ( $option, $value );
	my $h_parameter;
	foreach my $arg ( @args ){
		$option = $value = '';
		( $option, $value ) = split( /=/, $arg );
		if( !defined $value ){
			display_help();
		}
		$h_parameter->{$option} = $value;
	}
	my $h_merged = merge_parameter( read_ini( $ini_file ), $h_parameter );
	if(   !defined $h_merged->{'-file'}
	   || !defined $h_merged->{'-mode'})
	{
		display_help();
	}
	return $h_merged;
}


sub merge_parameter{
	my ( $h_ini_file, $h_parameter ) = @_;
	foreach my $key ( keys %$h_ini_file ){
		if( !defined $h_parameter->{$key} ){
			$h_parameter->{$key} = $h_ini_file->{$key};
		}
	}
	return $h_parameter;
}


sub display_help{
	print "+--------------------------------------------+\n";
	print "|   webdav.pl                                |\n";
	print "+--------------------------------------------+\n";
	print "|   1.HOW TO USE                             |\n";
	print "|      perl -w webdav.pl -mode=MODE          |\n";
	print "|       -file=FILE1[,FILE2,FILE3,...]        |\n";
	print "|       [-url=URL] [-user=USER] [-pass=PASS] |\n";
	print "|       [-path=LOCALPATH]                    |\n";
	print "|                                            |\n";
	print "|   2.ABOUT PARAMETERS                       |\n";
	print "|      -mode : PUT or GET.                   |\n";
	print "|      -file : file name list.               |\n";
	print "|      -url  : webdav server url.            |\n";
	print "|      -user : webdav login user.            |\n";
	print "|      -pass : webdav login pass             |\n";
	print "|      -path : local folder.                 |\n";
	print "|                                            |\n";
	print "|   3.ABOUT INI FILE                         |\n";
	print "|      you can write default parameter       |\n";
	print "|      to \"webdav.ini\" if you want.          |\n";
	print "|                                            |\n";
	print "|   4.EXAMPLES                               |\n";
	print "|      perl -w webdav.pl -path=./ -mode=put  |\n";
	print "|      -file=uke.txt,tkh.txt,mst.txt         |\n";
	print "|                                            |\n";
	print "+--------------------------------------------+\n";
	exit;
}


sub webdav_main{
	my ( @args ) = @_;
	my $h_parameter = get_parameter( './webdav.ini', @args );
	my @file_name_list = split( /,/, $h_parameter->{'-file'}, -1 );

	foreach my $file_name ( @file_name_list ){
		if( $h_parameter->{'-mode'} =~ /^get$/i ){
			get_file( $h_parameter, $file_name );
		}elsif( $h_parameter->{'-mode'} =~ /^put$/i ){
			if( -e $h_parameter->{'-path'} . $file_name ){
				put_file( $h_parameter, $file_name );
			}
		}
	}

}


sub put_file{
	my ( $h_parameter, $file_name ) = @_;
	my $dav = webdav_common( $h_parameter );

	# Put file
	$dav->put(-local=>$h_parameter->{'-path'}.$file_name, -url=>$h_parameter->{'-url'})
	or die "Put failed: " . $dav->message . "\n";
}


sub webdav_common{
	my ( $h_parameter ) = @_;
	my $dav = new HTTP::DAV;
	$dav->credentials(-user=>$h_parameter->{'-user'}, -pass=>$h_parameter->{'-pass'}, -url=>$h_parameter->{'-url'});
	$dav->open(-url=>$h_parameter->{'-url'}) or die "Cannot open $h_parameter->{'-url'}: " . $dav->message . "\n";
	return $dav;
}

sub get_file{
	my ( $h_parameter, $file_name ) = @_;
	my $dav = webdav_common( $h_parameter );
	print $h_parameter->{'-path'}."\n";
	# Get file
	$dav->get(-local=>$h_parameter->{'-path'}, -url=>$h_parameter->{'-url'}.$file_name)
	  or die "Get failed: " . $dav->message . "\n";
}

1;
